import { initData } from '@/api/data'

// 请求统一封装示例饿
export default {
    data() {
        return {
            listLoading: true, 
            params: {pageNo: 1,  
                pageSize:200,
                permissionGrade:1,
                name:''  
            }, 
            lists: [],
        }
    },
    methods: {
        async init() {
            if (!await this.beforeInit()) {
              return
            }
            return new Promise((resolve, reject) => {
                this.listLoading = true
                initData(this.params).then(res => {
                    if (res.code == 2000) {
                        let { list, total } = res.data
                        let data = list
                        let tree = []
                        for(let i=0;i<data.length;i++){
                            if(data[i].pid == '-2'){
                              let obj = data[i]
                              obj.children = []
                              tree.push(obj)
                              data.splice(i,1)
                              i--
                            }
                        }
                        menuList(tree)
                        function menuList(arr){
                            if(data.length !=0){
                              for(let i=0; i<arr.length;i++){
                                for(let j=0;j<data.length;j++){
                                  if(data[j].pid == arr[i].id){
                                    let obj = data[j]
                                    obj.children = []
                                    arr[i].children.push(obj)
                                    data.splice(j,1)
                                    j--
                                  }
                                }
                                menuList(arr[i].children)
                              }
                            }
                        }
                        this.lists = tree
                        this.listLoading = false
                        resolve(res)
                    }else{
                        this.$notify.error({
                            title: '错误',
                            message: '服务器出错'
                        });
                    }
                    
                }).catch(err => {
                    this.listLoading = false
                    reject(err)
                })
            })
        },
        beforeInit() {
            return true
        },
    }
}
